package ch0;

/**
 * @author yanrangnan
 * @date 2019/9/4
 */

public class ThreeThread {
    private static Object o = new Object();

    private static int i = 0;
    private static volatile boolean b1 = true;
    private static volatile boolean b2 = false;
    private static volatile boolean b3 = false;

    public static class T1 implements Runnable{
        public void run() {
            synchronized (o) {
                while (true) {
                    if(b1) {
                        i++;
                        System.out.println(Thread.currentThread().getName() + "正在执行" + i);
                        try {
                            Thread.sleep(100);
                            b1 = false;
                            b2 = true;
                            o.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }else {
                        System.out.println("b1else");
                        try {
                            o.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    o.notifyAll();
                }
            }
        }
    }
    public static class T2 implements Runnable{
        public void run() {
            synchronized (o) {
                while (true) {
                    if (b2) {
                        i++;
                        System.out.println(Thread.currentThread().getName() + "正在执行" + i);
                        try {
                            Thread.sleep(100);
                            o.wait();
                            b2 = false;
                            b3 = true;
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }else {
                        System.out.println("b2else");
                        try {
                            o.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    o.notifyAll();
                }
            }
        }
    }
    public static class T3 implements Runnable{
        public void run() {
            synchronized (o) {
                while (true) {
                    if (b3) {
                        i++;
                        System.out.println(Thread.currentThread().getName() + "正在执行" + i);
                        try {
                            Thread.sleep(100);
                            o.wait();
                            b3 = false;
                            b1 = true;
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }else {
                        System.out.println("b3else");
                        try {
                            o.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    o.notifyAll();
                }
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Thread t1 =new Thread(new T1());
        Thread t2 =new Thread(new T2());
        Thread t3 =new Thread(new T3());
        t1.setName("t1");t2.setName("t2");t3.setName("t3");
        t1.start();
        Thread.sleep(100);
        t2.start();
        t3.start();

    }
}
